package org.weasel.unittests;

import java.sql.Time;
import java.util.ArrayList;

import junit.framework.TestCase;

import org.weasel.business.Developer;
import org.weasel.business.Iteration;
import org.weasel.business.Project;
import org.weasel.business.Release;
import org.weasel.business.UserStory;
import org.weasel.business.Utilities;
import org.weasel.data.ProjectManager;

public class IterationTest extends TestCase {
	private final String xmlFileLoc = "./testfolder/IterationTest.xml";
	private Release release;
	private Project project;
	private ProjectManager manager;
	private Iteration iteration;
	private ArrayList<Developer> allDevelopers;
	private Developer developer;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		developer = new Developer("9999999", "developer", "mail", "position");
		allDevelopers = new ArrayList<Developer>();
		allDevelopers.add(developer);
		project = new Project(Utilities.createUniqueId(), "my project", xmlFileLoc, allDevelopers);
		manager = project.getProjectManager();
		release = project.createAndAddRelease("1st release");
		iteration = release.createAndAddIteration("1st iteration");
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		developer = null;
		allDevelopers = null;
		manager = null;
		iteration = null;
		project = null;
		release = null;
		Utilities.deleteFile(xmlFileLoc);
	}

	public void testUserStories() throws Exception {
		UserStory userStory = iteration.createAndAddUserStory("user story 1", 3, Time.valueOf("12:00:00"), Time.valueOf("13:00:00"), 20);
		
		//check if it is also in the XML file
		UserStory storyCheck = manager.getUserStory(userStory.getId());
		
		assertEquals("expected userstory (from XML) with id " + userStory.getId(), userStory.getId(), storyCheck.getId());
		assertEquals("expected userstory with id " + userStory.getId(), userStory.getId(), iteration.getUserStory(userStory.getId()).getId());
	}

	public void testRemoveUserStory() throws Exception {
		boolean ok = false;
		UserStory userStory = iteration.createAndAddUserStory("user story 1", 3, Time.valueOf("12:00:00"), Time.valueOf("13:00:00"), 20);
	
		iteration.removeUserStory(userStory.getId());
		//check if it has been removed from the XML file too
		try{
			manager.getUserStory(userStory.getId());
		}catch(NullPointerException e){
			ok = true;
		}
		assertEquals("expected true since story shouldn't be in the XML", true, ok);
		assertEquals("expected null since nothing should be retrieved", null, iteration.getUserStory(userStory.getId()));
	}

}
